package com.piao.springboot_mybatisplus.controller;

import com.piao.springboot_mybatisplus.entity.Word;
import com.piao.springboot_mybatisplus.mapper.WordMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
public class TestController {
    //存储预返回页面的结果对象
    private Map<String, Object> result;
    //注入业务对象
    @Resource
    private WordMapper wordMapper;

    //添加单词信息
    @PostMapping("/insertWord")
    @ResponseBody
    public Map<String, Object> insertWord(Word word) {
        result = new HashMap<>();

        //数据校验 判空！
        if (null == word) {
            result.put("message", "插入的单词为空");
            return result;
        }
        if (null == word.getChinese() || "".equals(word.getEnglish())) {
            result.put("message", "插入的单词中文为空或者为空字符串");
            return result;
        }
        if (null == word.getChinese() || "".equals(word.getChinese())) {
            result.put("message", "插入的单词中文为空或者为空字符串");
            return result;
        }
        wordMapper.insert(word);
        result.put("message", "插入单词成功");
        return result;
    }

    //根据id查询单词信息
    @GetMapping("/getWordById")
    @ResponseBody
    public Map<String, Object> getWordById(Integer id) {
        result = new HashMap<>();

        //数据校验 判空！
        if (null == id) {
            result.put("message", "传入id为空");
            return result;
        }

        result.put("word",wordMapper.selectById(id));
        result.put("message", "查询单词成功");
        return result;
    }


    //根据id更新单词信息
    @PostMapping("/updateWordById")
    @ResponseBody
    public Map<String, Object> updateWordById(Integer id, String english, String chinese) {
        result = new HashMap<>();

        //数据校验 判空！
        if (null == id) {
            result.put("message", "更新的单词id为空");
            return result;
        }
        if (null == english || "".equals(english)) {
            result.put("message", "更新的单词中文为空或者为空字符串");
            return result;
        }
        if (null == chinese || "".equals(chinese)) {
            result.put("message", "更新的单词中文为空或者为空字符串");
            return result;
        }
        wordMapper.updateById(new Word(id, english, chinese));
        result.put("message", "更新单词成功");
        return result;
    }

    //根据id删除单词信息
    @PostMapping("/deleteWordById")
    @ResponseBody
    public Map<String, Object> deleteWordById(Integer id) {
        result = new HashMap<>();
        //数据校验 判空！
        if (null == id) {
            result.put("message", "输入的单词id为空");
            return result;
        }
        wordMapper.deleteById(id);
        result.put("message", "删除单词成功");
        return result;
    }

    //查询所有单词
    @GetMapping("/getAllWord")
    @ResponseBody
    public Map<String, Object> getAllWord() {
        result = new HashMap<>();

        //selectList(null)设置过滤条件为空，就是查询所有，并以list形式返回
        result.put("word",wordMapper.selectList(null));
        result.put("message", "查询所有单词成功");
        return result;
    }
}
